<head><meta name="viewport" content="user-scalable=no,initial-scale=1,maximum-scale=1">
<meta charset=utf-8>
<style>
body { margin: 0px; }
canvas { width:100%; height:100%; overflow: hidden; }
</style>
<script type="text/javascript" src="../h3du_min.js"></script>
<script type="text/javascript" src="demoutil.js"></script>
<script type="text/javascript" src="../extras/meshjson.js"></script>
<script type="text/javascript" src="../extras/polyhedra.js"></script>
<script type="text/javascript" src="picking.js"></script>
<script type="text/javascript" src="../extras/camera.js"></script>
</head>
<body>
<canvas width="600" height="450" id=canvas></canvas>
<div style="position:absolute;left:0;top:0">
<input id=scalepicked type=checkbox><label for=scalepicked>Scale picked object</label>
</div>
<script id="demo">
/* global H3DU, raypick */
// <!--
/*
 Any copyright to this file is released to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/
 If you like this, you should donate
 to Peter O. (original author of
 the Public Domain HTML 3D Library) at:
 http://peteroupc.github.io/
*/

  // Create the 3D scene; find the HTML canvas and pass it
  // to Scene3D.
var scene = new H3DU.Scene3D(document.getElementById("canvas"))
   .cullFace(H3DU.Scene3D.BACK)
   .setClearColor("white");
var input = new H3DU.InputTracker(document.getElementById("canvas"));
var sub = new H3DU.Batch3D()
   .orthoAspect(-20, 20, -10, 10, -20, 20);
sub.getLights().setBasic();
var shapeGroup = new H3DU.ShapeGroup();
sub.addShape(shapeGroup);
var mesh;
mesh = H3DU.Polyhedra.hexahedron(4.5, 0);
var shape = new H3DU.Shape(mesh).setColor("red").setPosition(-15, 5, 0);
shapeGroup.addShape(shape);
mesh = H3DU.Polyhedra.octahedron(4.5, 0);
shapeGroup.addShape(
    new H3DU.Shape(mesh).setColor("blue").setPosition(-5, 5, 0));
mesh = H3DU.Polyhedra.icosahedron(4.5, 0);
shapeGroup.addShape(
    new H3DU.Shape(mesh).setColor("lime").setPosition(5, 5, 0));
mesh = H3DU.Polyhedra.tetrahedron(4.5, 0);
shapeGroup.addShape(
    new H3DU.Shape(mesh).setColor("darkorange").setPosition(15, 5, 0));
mesh = H3DU.Polyhedra.dodecahedron(4.5, 0);
shapeGroup.addShape(
    new H3DU.Shape(mesh).setColor("yellow").setPosition(-15, -5, 0));
var pickdot = new H3DU.Shape(H3DU.Meshes.createSphere(0.5))
  .setColor("green").setVisible(false);
var pickdotShape = null;
var pickdotLocal = null;
var rotation = [0, 0, 0];
var timer = {};
var objects = [];
for(var i = 0; i < shapeGroup.shapeCount(); i++) {
  objects.push(shapeGroup.getShape(i));
}
shapeGroup.addShape(pickdot);
var lastPress = false;
function scalePickedObject() {
  "use strict";
  return document.getElementById("scalepicked").checked;
}
setInterval(function() {
  "use strict";
  input.update();
  if(input.leftButton) {
    if(lastPress === false) {
    // Left button was just pressed
      var mousePos = input.mousePos();
      var viewport = [0, 0, scene.getWidth(), scene.getHeight()];
      var projview = sub.getProjectionViewMatrix();
      var o = raypick(mousePos.cx, mousePos.cy, projview, viewport, objects);
      if(o.index >= 0) {
        pickdotShape = objects[o.index];
        pickdotLocal = o.local;
        pickdot.setVisible(true);
      } else {
        pickdot.setVisible(false);
        pickdotShape = null;
      }
      lastPress = true;
    }
  } else {
    lastPress = false;
  }
}, 20);
H3DU.renderLoop(function(time) {
  "use strict";
  for(var i = 0; i < shapeGroup.shapeCount(); i++) {
    var shape = shapeGroup.getShape(i);
    shape.setQuaternion(
      H3DU.Math.quatFromTaitBryan(rotation));
    if(shape === pickdotShape && scalePickedObject()) {
      shape.getTransform().setScale(1.5);
    } else {
      shape.getTransform().setScale(1.0);
    }
  }
  if(pickdotShape) {
    var matrix = pickdotShape.getMatrix();
    var world = H3DU.Math.mat4projectVec3(matrix, pickdotLocal);
    pickdot.setPosition(world);
  }
  rotation[0] = 360 * H3DU.getTimePosition(timer, time, 6000);
  rotation[1] = 360 * H3DU.getTimePosition(timer, time, 12000);
  scene.render(sub);
});
// -->
</script>
</body>
